<template>
    <div class="wrap ">
        <Skeleton :loading="loading">
            <div class="top-search">
                <el-autocomplete v-model="city" value-key="city" :fetch-suggestions="querySearch" :trigger-on-focus="false"
                    placeholder="选择其他城市" @select="handleSelect" size="default">
                    <template #default="{ item }">
                        <span> {{ item.province }}&nbsp;&nbsp;&nbsp;{{ item.city }}</span>
                    </template>
                </el-autocomplete>
                <div class="search" @click="search">搜 索</div>
            </div>
            <div class="content">
                <!-- 日期 -->
                <div class="day">
                    <div @click="toogle(0)" :class="{ active: state.active === 0 }">今天</div>
                    <div @click="toogle(1)" :class="{ active: state.active === 1 }">明天</div>
                    <div @click="toogle(2)" :class="{ active: state.active === 2 }">后天</div>
                </div>
                <!-- 天气 -->
                <div class="weather">
                    <svg-icon class="svg" :name="weatherIcon"></svg-icon>
                    <div class="weather-text">
                        <div class="temperature">{{ state.todayWeather?.tempMax }} ℃</div>
                        <div class="weather-type">{{ state.todayWeather?.textDay }}</div>
                    </div>
                </div>
                <!-- 天气详细信息 -->
                <div class="other">
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="fengxiang"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.windScaleDay }}级</span>
                            <span>{{ state.todayWeather?.windDirDay }}</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="fsux_tubiao_fenbuquxiantu"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.uvIndex }}</span>
                            <span>紫外线</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="gaowen"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.tempMax }}℃</span>
                            <span>最高温</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="diwen"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.tempMin }}℃</span>
                            <span>最低温</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="gary"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.vis }}km</span>
                            <span>能见度</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="shidu1"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.humidity }}%</span>
                            <span>相对湿度</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="yun"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.cloud }}</span>
                            <span>云量</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="jiangshui"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.precip }}mm</span>
                            <span>降水量</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" color="#049eff" name="daqiyali"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.pressure }}Pa</span>
                            <span>大气压强</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="tianqi-fengxiang"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.wind360Night }}</span>
                            <span>风向角度</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" :name="state.todayWeather?.moonPhaseIcon"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.moonPhase }}</span>
                            <span>月相</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="manyue"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.moonrise }}</span>
                            <span>月升时间</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="erhaiyue"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.moonset === "" ? "未知" :
                                state.todayWeather?.moonset
                            }}</span>
                            <span>月落时间</span>
                        </div>
                    </div>

                    <div class="other-list">
                        <svg-icon class="svg-icon" name="richu"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.sunrise }}</span>
                            <span>日出时间</span>
                        </div>
                    </div>
                    <div class="other-list">
                        <svg-icon class="svg-icon" name="rila"></svg-icon>
                        <div class="weather-details">
                            <span class="info">{{ state.todayWeather?.sunset }}</span>
                            <span>日落时间</span>
                        </div>
                    </div>
                </div>
            </div>
            <h1 class="city">{{ state.title }}</h1>
        </Skeleton>
    </div>
</template>
<script lang='ts' setup>
import { getWeatherInfo, getCityList } from '@/api/other/weather';
import { handleResponse } from '@/utils/responseCode';
import { computed, onMounted, reactive, ref } from 'vue';
import Skeleton from '@/components/Skeleton/index.vue'

export interface WeatherItem {
    cloud: string, // 云量
    fxDate: string,// 预报日期
    humidity: string, //  相对湿度,百分比数值
    iconDay: string, //预报白天天气状况的图标代码
    iconNight: string, // 预报夜间天气状况的图标代码
    moonPhase: string, // 月相名称
    moonPhaseIcon: string, // 月相图标代码
    moonrise: string, // 月升时间
    moonset: string, // 月落时间
    precip: string, // 预报当天总降水量，默认单位：毫米
    pressure: string, // 大气压强，默认单位：百帕
    sunrise: string,//日出时间
    sunset: string, // 日落时间
    tempMax: string, // 当天最高温
    tempMin: string, // 当天最低温
    textDay: string, // 白天天气状况文字描述
    textNight: string, // 晚上天气状况文字描述
    uvIndex: string, // 紫外线强度指数
    vis: string, // 能见度 公里
    wind360Day: string, // 白天风向360角度
    wind360Night: string, // 晚上风向360角度
    windDirDay: string, // 预报白天风向
    windDirNight: string, // 预报晚上风向
    windScaleDay: string, //  预报白天风力等级
    windScaleNight: string,//  预报晚上风力等级
    windSpeedDay: string, // 预报白天风速，公里/小时
    windSpeedNight: string,// 预报晚间风速，公里/小时
}

interface IState {
    active: number
    todayWeather: WeatherItem | null
    allWeather: WeatherItem[]
    city: CityList
    title: string
}
interface CityList {
    locationId: number
    province: string
    city: string
}
let city = ref("")
const loading = ref(true)
const state = reactive<IState>({
    active: 0,
    todayWeather: null,
    allWeather: [],
    city: {
        locationId: 101110101,
        province: "陕西",
        city: "西安"
    },
    title: "西安"
})

let weatherIcon = computed(()=>{
    return state.todayWeather?.textDay === "阴" ? "yintian":state.todayWeather?.textDay=== "晴" ? "qingtian":state.todayWeather?.textDay === "雨" ?"jiangshui":state.todayWeather?.textDay === "雪" ? "icon-shengdantubiao-22":"tianqi"
})

const toogle = (num: number) => {
    state.active = num
    state.todayWeather = state.allWeather[num]
}

// 默认获取西安的天气
const getWether = async (locationId: number = 101110101) => {
    loading.value = true
    let res = await getWeatherInfo({ location: locationId })

    if (!handleResponse(res.data.code)) return
    loading.value = false
    state.allWeather = res.data.daily
    state.title = state.city.city
    state.todayWeather = res.data.daily[state.active]
}

getWether(state.city.locationId)
const restaurants = ref<CityList[]>([])
// 获取所有城市列表
const getAllCity = async () => {
    let res = await getCityList()
    restaurants.value = res.data
}

onMounted(() => {
    getAllCity()
})

// 查询方法
const querySearch = (queryString: string, cb: any) => {

    const results = queryString ?
        restaurants.value.filter((item) => item.province.includes(city.value) || item.city.includes(city.value))
        : restaurants.value

    cb(results)
}

const handleSelect = (item: CityList) => {
    state.city = item
}

const search = () => {

    if (!city.value || city.value === "") return

    getWether(state.city.locationId)


}
</script>
<style lang="less" scoped>
.wrap {
    background-image: url('@/assets/weather.png');
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--background);
    position: relative;
    overflow: hidden;

    .top-search {
        position: absolute;
        top: 5%;
        left: 50%;
        transform: translateX(-50%);
        display: flex;

        :deep(.el-input__wrapper) {
            color: var(--active-color);
        }

        .search {
            width: 80px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            background-color: var(--active-color);
            color: #fff;
            border-radius: 30px;
            margin-left: 25px;
            cursor: pointer;
            user-select: none;

            &:hover {
                opacity: 0.8;
            }

            &:active {
                transform: scale(0.95);
            }
        }
    }

    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        height: 100%;
        padding-top: 130px;

        .day {
            display: flex;

            div {
                color: #fff;
                width: 150px;
                height: 45px;
                margin: 0px 5px;
                border-radius: 30px;
                text-align: center;
                line-height: 45px;
                box-shadow: rgba(9, 30, 66, 0.25) 0px 4px 8px -2px, rgba(9, 30, 66, 0.08) 0px 0px 0px 1px;
                font-size: 18px;
                letter-spacing: 8px;
                cursor: pointer;
            }
        }

        .weather {
            display: flex;
            margin: 50px 0px;

            .svg {
                width: 100px;
                height: 100px;
                margin-right: 20px;
            }

            .weather-text {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                padding-top: 5px;
                color:#fff;
                .temperature {
                    font-size: 30px;
                }

                .weather-type {
                    font-size: 18px;
                }


            }

        }

        .other {
            width: 50%;
            min-width: 700px;
            height: 60px;
            overflow: hidden;
            display: flex;
            flex-wrap: wrap;
            flex-shrink: 0;
            background-color: rgba(0, 0, 0, 0.5);
            padding: 10px 0px;
            border-radius: 10px;
            color: var(--background);
            transition: all .3s;
            cursor: pointer;

            &:hover {
                height: 160px;
            }

            .other-list {
                display: flex;
                align-items: center;
                padding: 0px 20px;
                width: calc(20%);
                overflow: hidden;

                &:nth-child(n+6) {
                    padding: 20px 20px 0px 20px;
                }

                .weather-details {
                    display: flex;
                    flex-direction: column;

                    span {
                        font-size: 12px;
                        color: #ccc;
                    }

                    .info {
                        font-size: 14px;
                        color: var(--color);
                    }
                }

                .svg-icon {
                    width: 30px;
                    height: 30px;
                    margin-right: 5px;
                    color: #ccc;
                }
            }
        }
    }

    .city {
        position: absolute;
        left: 5%;
        top: 5%;
        letter-spacing: 30px;
        color:var(--active-color);
        opacity: 0.5;
        font-size: 80px;
        user-select: none;
    }
}

.active {
    background-color: var(--active-color);
}
</style>
